package com.settlement.system.service.impl;

import com.settlement.system.model.entity.BsOmsReturnOrderSyncRecord;
import com.settlement.system.mapper.BsOmsReturnOrderSyncRecordMapper;
import com.settlement.system.service.BsOmsReturnOrderSyncRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.BsOmsReturnOrderSyncRecordQuery;
import com.settlement.system.model.form.BsOmsReturnOrderSyncRecordForm;
import org.springframework.beans.BeanUtils;
import com.settlement.system.model.vo.BsOmsReturnOrderSyncRecordImportVo;

/**
 * <p>
 * 百盛退货单同步记录表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-09-06
 */
@Service
public class BsOmsReturnOrderSyncRecordServiceImpl extends ServiceImpl<BsOmsReturnOrderSyncRecordMapper, BsOmsReturnOrderSyncRecord> implements BsOmsReturnOrderSyncRecordService {

    @Override
    public Page<BsOmsReturnOrderSyncRecord> getPage(BsOmsReturnOrderSyncRecordQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<BsOmsReturnOrderSyncRecord> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveBsOmsReturnOrderSyncRecord(BsOmsReturnOrderSyncRecordForm bsOmsReturnOrderSyncRecordForm) {
         BsOmsReturnOrderSyncRecord bsOmsReturnOrderSyncRecord = new BsOmsReturnOrderSyncRecord();
         BeanUtils.copyProperties(bsOmsReturnOrderSyncRecordForm, bsOmsReturnOrderSyncRecord);
         return this.saveOrUpdate(bsOmsReturnOrderSyncRecord);
     }

     @Override
     public BsOmsReturnOrderSyncRecordForm getBsOmsReturnOrderSyncRecordFormData(Long id) {
         BsOmsReturnOrderSyncRecord bsOmsReturnOrderSyncRecord = this.getById(id);
         BsOmsReturnOrderSyncRecordForm bsOmsReturnOrderSyncRecordForm = new BsOmsReturnOrderSyncRecordForm();
         BeanUtils.copyProperties(bsOmsReturnOrderSyncRecord, bsOmsReturnOrderSyncRecordForm);
         return bsOmsReturnOrderSyncRecordForm;
     }

     @Override
     public boolean deleteBsOmsReturnOrderSyncRecord(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<BsOmsReturnOrderSyncRecordImportVo> voList)  {
        List<BsOmsReturnOrderSyncRecord> bsOmsReturnOrderSyncRecordList = new ArrayList<>();
        for (BsOmsReturnOrderSyncRecordImportVo importVo : voList) {
            BsOmsReturnOrderSyncRecord bsOmsReturnOrderSyncRecord = new BsOmsReturnOrderSyncRecord();
            BeanUtils.copyProperties(importVo, bsOmsReturnOrderSyncRecord);
            bsOmsReturnOrderSyncRecordList.add(bsOmsReturnOrderSyncRecord);
        }
        this.saveOrUpdateBatch(bsOmsReturnOrderSyncRecordList);
    }

    @Override
    public List<BsOmsReturnOrderSyncRecordImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<BsOmsReturnOrderSyncRecord> bsOmsReturnOrderSyncRecordList = this.getBaseMapper().selectBatchIds(ids);
        List<BsOmsReturnOrderSyncRecordImportVo> voList = new ArrayList<>();
        for (BsOmsReturnOrderSyncRecord bsOmsReturnOrderSyncRecord : bsOmsReturnOrderSyncRecordList) {
            BsOmsReturnOrderSyncRecordImportVo vo = new BsOmsReturnOrderSyncRecordImportVo();
            BeanUtils.copyProperties(bsOmsReturnOrderSyncRecord, vo);
            voList.add(vo);
        }
        return voList;
    }
}
